/*******************************************************************************
Paper title: Does early nutrition predict cognitive skills during later childhood?
Evidence from two developing countries
Alan Sanchez, Marta Favara, Margaret Sheridan, Jere Behrman.
Created: 8 OCt 2020
This version: 4 Dec 2023 
Structure: Estimations use adjusted HAZ/stunting and siblings-difference OLS methods
Content: Table C3
*******************************************************************************/
			
clear all
local user Alan

if "`user'"=="Alan" {
global outdata	C:\Users\alans\Dropbox\_NIHR21_Proposal\NIH Papers\Paper 1_nutrition&EF\paper\WD\Replication files\Data
global output   C:\Users\alans\Dropbox\_NIHR21_Proposal\NIH Papers\Paper 1_nutrition&EF\paper\WD\Replication files\Output
}

global control0  chage_r4 chage_r4_2 female dm_educ2 dm_educ3 dm_educ4 urban_c /*singleparent_r1*/  hhsize_c
global country1  mom_spanish
global country2  mom_oromifa mom_tigrina mom_other

global control2q wi_cq2 wi_cq3 wi_cq4 wi_cq5

global control3q educexp_all_r2q2 educexp_all_r2q3 educexp_all_r2q4 educexp_all_r2q5
global control4q nfoodexp_r2q2 nfoodexp_r2q3 nfoodexp_r2q4 nfoodexp_r2q5
	
global task3 	itask3
global task2 	itask2_a
global task1 	task1
global task4 	itask4	

global btask3 	bitask3
global btask2 	bitask2_a
global btask1 	btask1
global btask4 	bitask4	

global alltask 	hr2 hr3 wkend /*practices*/
		
global cluster_r1 sib_y sib_o /*dclustid_r11- dclustid_r120*/
		
*global specific to model 1b
global control1b zhfa_r3 preprim_with
global control1_stuntr3 stunting_r3 preprim_with
global control1_stuntlvlr3 stunting_r3_lvl2 stunting_r3_lvl3 preprim_with

global sample1  zhfa_c<=6 & zhfa_c>=-6
global sample2  zhfa_c<=6 & zhfa_c>=-6

global absorb   childid
	
*---------------------------------------------------------------------------------------------------------------------*---------------------------------------------------------------------------------------------------------------------*------------------------------------------------------------------------------------------------------------------*/

use "$outdata\pe_et_childlevel_230821", clear

sort childid
merge childid using "$outdata\firstborn.dta"
tab _merge
drop if _merge==2
drop _merge

***Requires for pooled sample estimations 
replace mom_spanish=0 if country==2 
replace mom_oromifa=0 if country==1  
replace mom_tigrina=0 if country==1 
replace mom_other=0   if country==1 

***Different cluster IDs are required for the pooled sample of two countries
replace clustid_r1=clustid_r1+100 if country==2

global country   mom_spanish mom_oromifa mom_tigrina mom_other

bysort childid sibling: egen n=max(task) /* keep only paired-siblings who answer all tasks*/
drop if n!=4 /*22 obs*/
drop n
est clear

egen hhgroup=group(childid)
xtset hhgroup

gen sib_y=0
gen sib_o=0
replace sib_y=1 if sibling==1 & sibyounger==1
replace sib_o=1 if sibling==1 & sibyounger==0 

gen zhfa_c=.
replace zhfa_c=zhfa_r3 if sibling==1
replace zhfa_c=zhfa_r2 if sibling==0

gen stunting_c=.
replace stunting_c=stunting_r3 if sibling==1
replace stunting_c=stunting_r2 if sibling==0

gen zhfaxsib_y=zhfa_c*sib_y
gen zhfaxsib_o=zhfa_c*sib_o
gen zhfaxage=zhfa_c*chage_r4

gen stuntingxsib_y=stunting_c*sib_y
gen stuntingxsib_o=stunting_c*sib_o
gen stuntingxage  =stunting_c*chage_r4

gen zhfaxfem=zhfa_c*female

gen stuntingxfem=stunting_c*female

gen chage_r4_2=chage_r4*chage_r4

rename euclideandave_delay_0 euclid_del 
rename euclideandave_dot_0   euclid_dot		
	
label var stunting_c     "Stunted"
label var stuntingxsib_y "Stunted x younger sib"
label var stuntingxsib_o "Stunted x older sib"
label var stuntingxfem   "Stunted x female"

label var zhfa_c         "Height for age"
label var zhfaxsib_y     "Height for age x younger sib"
label var zhfaxsib_o     "Height for age x older sib"
label var zhfaxfem       "Height for age x female"

label var chage_r4     "Age in months, r4"
label var chage_r4_2   "Age in months squared, r4"
label var female       "Child is female"
label var dm_educ2     "Maternal edu: complete primary"
label var dm_educ3     "Maternal edu: complete secondary"
label var dm_educ4     "Maternal edu: complete tertiary"
label var urban_r1     "Urban area, r1"
label var hhsize_r2    "Household size, r2" 
label var mom_spanish  "Maternal native tongue: spanish"
label var mom_oromifa  "Maternal native tongue: oromifah" 
label var mom_tigrina  "Maternal native tongue: tigrina"
label var mom_other    "Maternal native tongue: other"
label var wi_cq2      "Wealth index quintile 2"
label var wi_cq3      "Wealth index quintile 3"
label var wi_cq4      "Wealth index quintile 4"
label var wi_cq5      "Wealth index quintile 5"

********************************************************************************
*** Adjusted HAZ for older siblings in Ethiopia
********************************************************************************
sort  country childid sibling

merge country childid sibling using "$outdata\lasso_prediction_older.dta"
drop _merge

replace zhfa_c      =zhfa_5_p               if sibling==1 & sibyounger==0 & country==2
replace zhfaxsib_y  =zhfa_c*sib_y           if sibling==1 & sibyounger==0 & country==2
replace zhfaxsib_o  =zhfa_c*sib_o           if sibling==1 & sibyounger==0 & country==2
replace zhfaxage    =zhfa_c*chage_r4        if sibling==1 & sibyounger==0 & country==2

tab     stunting_c                 		    if sibling==1 & sibyounger==0 & country==2
replace stunting_c    =1          		    if zhfa_c<-2  & zhfa_c!=. & sibling==1 & sibyounger==0 & country==2
replace stunting_c    =0          		    if zhfa_c>=-2 & zhfa_c!=. & sibling==1 & sibyounger==0 & country==2
replace stuntingxsib_y=stunting_c*sib_y  	if sibling==1 & sibyounger==0 & country==2
replace stuntingxsib_o=stunting_c*sib_o   	if sibling==1 & sibyounger==0 & country==2
replace stuntingxage  =stunting_c*chage_r4 	if sibling==1 & sibyounger==0 & country==2
tab     stunting_c                   		if sibling==1 & sibyounger==0 & country==2

********************************************************************************
*** Adjusted for index children and younger siblings in Peru & Ethiopia
********************************************************************************
sort  country childid sibling
merge country childid sibling using "$outdata\prediction_index_younger.dta"
drop _merge

replace zhfa_c      =zhfa_5_p1              if sibyounger!=0
replace zhfaxsib_y  =zhfa_c*sib_y           if sibyounger!=0
replace zhfaxsib_o  =zhfa_c*sib_o           if sibyounger!=0
replace zhfaxage    =zhfa_c*chage_r4        if sibyounger!=0

tab     stunting_c                 		    if sibyounger!=0
replace stunting_c    =1          		    if zhfa_c<-2  & zhfa_c!=. & sibyounger!=0
replace stunting_c    =0          		    if zhfa_c>=-2 & zhfa_c!=. & sibyounger!=0
replace stuntingxsib_y=stunting_c*sib_y  	if sibyounger!=0
replace stuntingxsib_o=stunting_c*sib_o   	if sibyounger!=0
replace stuntingxage  =stunting_c*chage_r4 	if sibyounger!=0
tab     stunting_c                   		if sibyounger!=0

keep if $sample1
global variables firstborn zhfa_c stunting_c ///
chage_r4 chage_r4_2 female dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c ///
wi_cq2 wi_cq3 wi_cq4 wi_cq5 ///
hr2 hr3 wkend //// 
mom_spanish mom_oromifa mom_tigrina mom_other country ///
sib_y sib_o
keep childid sibling $variables $task3 $task2 $task1 $task4 $btask3 $btask2 $btask1 $btask4

by childid, sort: egen abc=sum(sibling)
tab abc
drop if abc==0
drop abc

****************************
*** Table C3
****************************

****TASK 3 / WM***
preserve
keep if $task3!=.
reshape wide $variables $task3 $btask3, i(childid) j(sibling)
gen d_task3      =itask31-itask30
gen d_btask3     =bitask31-bitask30

gen d_zhfa_c     =zhfa_c1-zhfa_c0
gen d_stunting_c =stunting_c1-stunting_c0
gen d_hr2        =hr21-hr20
gen d_hr3        =hr31-hr30
gen d_wkend      =wkend1-wkend0
gen d_age        =chage_r41-chage_r40
gen d_wi_cq2     =wi_cq21-wi_cq20
gen d_wi_cq3 	 =wi_cq31-wi_cq30
gen d_wi_cq4     =wi_cq41-wi_cq40
gen d_wi_cq5     =wi_cq51-wi_cq50
gen d_sexfm =0
gen d_sexmf =0
gen d_sexff =0
replace d_sexfm =1 if female0==1 & female1==0
replace d_sexmf =1 if female0==0 & female1==1
replace d_sexff =1 if female0==1 & female1==1

*Stunting
reg d_task3 d_stunting_c d_btask3 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0
estimates store reg4
reg d_task3 d_stunting_c d_btask3 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0 if country0==1
estimates store reg5
reg d_task3 d_stunting_c d_btask3 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0 if country0==2
estimates store reg6

xml_tab reg4 reg5 reg6, ///
save("$output\reg_071023_5.xls") replace tstat below sheet("TableC3_WM") stats(N r2_a)
restore

****TASK 2 / IC***
preserve
keep if $task2!=.
reshape wide $variables $task2 $btask2, i(childid) j(sibling)
gen d_task2      =itask2_a1-itask2_a0
gen d_btask2     =bitask2_a1-bitask2_a0

gen d_zhfa_c     =zhfa_c1-zhfa_c0
gen d_stunting_c =stunting_c1-stunting_c0
gen d_hr2        =hr21-hr20
gen d_hr3        =hr31-hr30
gen d_wkend      =wkend1-wkend0
gen d_age        =chage_r41-chage_r40
gen d_wi_cq2     =wi_cq21-wi_cq20
gen d_wi_cq3 	 =wi_cq31-wi_cq30
gen d_wi_cq4     =wi_cq41-wi_cq40
gen d_wi_cq5     =wi_cq51-wi_cq50
gen d_sexfm =0
gen d_sexmf =0
gen d_sexff =0
replace d_sexfm =1 if female0==1 & female1==0
replace d_sexmf =1 if female0==0 & female1==1
replace d_sexff =1 if female0==1 & female1==1

*Stunting
reg d_task2 d_stunting_c d_btask2 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0
estimates store reg4
reg d_task2 d_stunting_c d_btask2 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0 if country0==1
estimates store reg5
reg d_task2 d_stunting_c d_btask2 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0 if country0==2
estimates store reg6

xml_tab reg4 reg5 reg6, ///
save("$output\reg_071023_5.xls") append tstat below sheet("TableC3_IC") stats(N r2_a)
restore

****TASK 1 / LM***
preserve
keep if $task1!=.
reshape wide $variables $task1 $btask1, i(childid) j(sibling)
gen d_task1      =task11-task10
gen d_btask1     =btask11-btask10

gen d_zhfa_c     =zhfa_c1-zhfa_c0
gen d_stunting_c =stunting_c1-stunting_c0
gen d_hr2        =hr21-hr20
gen d_hr3        =hr31-hr30
gen d_wkend      =wkend1-wkend0
gen d_age        =chage_r41-chage_r40
gen d_wi_cq2     =wi_cq21-wi_cq20
gen d_wi_cq3 	 =wi_cq31-wi_cq30
gen d_wi_cq4     =wi_cq41-wi_cq40
gen d_wi_cq5     =wi_cq51-wi_cq50
gen d_sexfm =0
gen d_sexmf =0
gen d_sexff =0
replace d_sexfm =1 if female0==1 & female1==0
replace d_sexmf =1 if female0==0 & female1==1
replace d_sexff =1 if female0==1 & female1==1

*Stunting
reg d_task1 d_stunting_c d_btask1 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0
estimates store reg4
reg d_task1 d_stunting_c d_btask1 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0 if country0==1
estimates store reg5
reg d_task1 d_stunting_c d_btask1 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0 if country0==2
estimates store reg6

xml_tab reg4 reg5 reg6, ///
save("$output\reg_071023_5.xls") append tstat below sheet("TableC3_LM") stats(N r2_a)
restore

****TASK 4 / IL***
preserve
keep if $task4!=.
reshape wide $variables $task4 $btask4, i(childid) j(sibling)
gen d_task4      =itask41-itask40
gen d_btask4     =bitask41-bitask40

gen d_zhfa_c     =zhfa_c1-zhfa_c0
gen d_stunting_c =stunting_c1-stunting_c0
gen d_hr2        =hr21-hr20
gen d_hr3        =hr31-hr30
gen d_wkend      =wkend1-wkend0
gen d_age        =chage_r41-chage_r40
gen d_wi_cq2     =wi_cq21-wi_cq20
gen d_wi_cq3 	 =wi_cq31-wi_cq30
gen d_wi_cq4     =wi_cq41-wi_cq40
gen d_wi_cq5     =wi_cq51-wi_cq50
gen d_sexfm =0
gen d_sexmf =0
gen d_sexff =0
replace d_sexfm =1 if female0==1 & female1==0
replace d_sexmf =1 if female0==0 & female1==1
replace d_sexff =1 if female0==1 & female1==1

*Stunting
reg d_task4 d_stunting_c d_btask4 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0
estimates store reg4
reg d_task4 d_stunting_c d_btask4 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0 if country0==1
estimates store reg5
reg d_task4 d_stunting_c d_btask4 d_hr2 d_hr3 d_wkend d_wi_cq2 d_wi_cq3 d_wi_cq4 d_wi_cq5 d_age d_sexfm d_sexmf d_sexff firstborn0 if country0==2
estimates store reg6

xml_tab reg4 reg5 reg6, ///
save("$output\reg_071023_5.xls") append tstat below sheet("TableC3_IL") stats(N r2_a)
restore
